package com.baselibrary.view;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.utils.DensityUtil;


public class MarginDecoration extends RecyclerView.ItemDecoration {

    private int margin;

    public MarginDecoration(Context context, int value) {
        margin = DensityUtil.dip2px(context, value);
    }

    @Override

    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    //由于每行都只有2个，所以第一个都是2的倍数，把左边距设为0
        if (parent.getChildLayoutPosition(view) % 2 == 0) {
            outRect.set(margin, 0, margin, 0);
        } else {
            outRect.set(0, 0, margin, 0);
        }
    }
}